import inherits from 'inherits-browser';
import KeyboardBindings from './customKeyboardBindings';

export default function BpmnKeyboardBindings(this: any, injector: any) {
    injector.invoke(KeyboardBindings, this);
}

inherits(BpmnKeyboardBindings, KeyboardBindings);

BpmnKeyboardBindings.$inject = [
    'injector',
    'selection',
    'modeling',
    'customRenderer',
];

BpmnKeyboardBindings.prototype.registerBindings = function (keyboard: any, editorActions: any, selection: any, modeling: any, customRenderer: any) {
    KeyboardBindings.prototype.registerBindings.call(this, keyboard, editorActions, selection, modeling, customRenderer);
    const addListener = (action: any, fn: Function) => {
        if (editorActions.isRegistered(action)) {
            keyboard.addListener(fn);
        }
    }

    // 全选
    // CTRL + A
    addListener('selectElements', function (context: { keyEvent: any; }) {
        const event = context.keyEvent;
        if (keyboard.isKey(['a', 'A'], event) && keyboard.isCmd(event)) {
            try { editorActions.trigger('selectElements') } catch (error) { };
            return true;
        }
    });

    // 按id或名称搜索节点元素
    // CTRL + F
    addListener('find', function (context: { keyEvent: any; }) {
        const event = context.keyEvent;
        if (keyboard.isKey(['f', 'F'], event) && keyboard.isCmd(event)) {
            try { editorActions.trigger('find') } catch (error) { };
            return true;
        }
    });

    // 空间工具
    // S
    addListener('spaceTool', function (context: { keyEvent: any; }) {
        const event = context.keyEvent;
        if (keyboard.hasModifier(event)) {
            return;
        }
        if (keyboard.isKey(['s', 'S'], event)) {
            try { editorActions.trigger('spaceTool') } catch (error) { };
            return true;
        }
    });

    // 套索工具
    // L
    addListener('lassoTool', function (context: { keyEvent: any; }) {
        const event = context.keyEvent;
        if (keyboard.hasModifier(event)) {
            return;
        }
        if (keyboard.isKey(['l', 'L'], event)) {
            try { editorActions.trigger('lassoTool') } catch (error) { };
            return true;
        }
    });

    // 手工具
    // H
    addListener('handTool', function (context: { keyEvent: any; }) {
        const event = context.keyEvent;
        if (keyboard.hasModifier(event)) {
            return;
        }
        if (keyboard.isKey(['h', 'H'], event)) {
            try { editorActions.trigger('handTool') } catch (error) { };
            return true;
        }
    });

    // 连接线工具（先点击c再点击需要连接的两个节点元素）
    // C
    addListener('globalConnectTool', function (context: { keyEvent: any; }) {
        const event = context.keyEvent;
        if (keyboard.hasModifier(event)) {
            return;
        }
        if (keyboard.isKey(['c', 'C'], event)) {
            try { editorActions.trigger('globalConnectTool') } catch (error) { };
            return true;
        }
    });
};